﻿var matranhacungcap = "", matranhacungcap_chitiet = "";
$(function () {
    $("#ddlKho_Khoa").parent().find(".dropdown-toggle").focus();
    $("#txtTuNgay").data("DateTimePicker").setMaxDate($("#txtDenNgay").val());
    $("#txtDenNgay").data("DateTimePicker").setMinDate($("#txtTuNgay").val());
    // Change value cho textbox từ ngày, đến ngày
    $("#txtTuNgay").on("dp.change", function (e) {
        $("#txtDenNgay").data("DateTimePicker").setMinDate(e.date);
    });
    $("#txtDenNgay").on("dp.change", function (e) {
        $("#txtTuNgay").data("DateTimePicker").setMaxDate(e.date);
    });
    // Enter của textbox đến ngày
    $("#txtDenNgay").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnTimKiem").focus();
        }
    });
    // Event khi đóng modal bootstrap
    $("#div_phieutranhacungcap").on("hide", function () {
        window.__doPostBack("btnTimKiem_Click", "");
    });
    // Enter của textbox ghi chú
    $("#txtDienGiai").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnLuuPhieu").focus();
        }
    });
    // Enter của textbox số lượng
    $("#txtSoLuong").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnLuuPhieu_ChiTiet").focus();
        }
    });
    // Event change combobox vật tư
    $("#ddlVatTu").on("change", function () {
        var data = SelectVatTuById($(this).val());
        if (data.length <= 0) return;
        $("#ddDonVi").selectpicker("val", data[0].MaDonVi);
        $("#ddDonVi").selectpicker("refresh");
        filldropdown(
            "#ddlLoSanXuat",
            PhieuTraNhaCungCap_LoSanXuat_ByMaVatTu(
                $("#ddNhaCungCap option:selected").val(), $("#ddlKho option:selected").val(),
                $("#ddlVatTu option:selected").val()
            )
        );
        $("#ddlLoSanXuat").parent().find(".dropdown-toggle").focus();
        filldropdown("#ddlPhieuNhap", []);
        $("#txtDonGia").val("");
        $("#txtHanSuDung").val("");
        $("#txtSoLuongTon").val("");
    });
    // Event change combobox lô sản xuất
    $("#ddlLoSanXuat").on("change", function () {
        $("#ddlPhieuNhap").parent().find(".dropdown-toggle").focus();
        filldropdown(
            "#ddlPhieuNhap",
            PhieuTraNhaCungCap_MaPhieuNhap_By_LoSanXuat(
                $("#ddNhaCungCap option:selected").val(), $("#ddlKho option:selected").val(),
                $("#ddlVatTu option:selected").val(), $("#ddlLoSanXuat option:selected").val()
            )
        );
        $("#txtDonGia").val("");
        $("#txtHanSuDung").val("");
        $("#txtSoLuongTon").val("");
    });
    // Event change combobox phiếu nhập
    $("#ddlPhieuNhap").on("change", function () {
        var data = PhieuTraNhaCungCap_DonGia_ByMaPhieuNhap(
            $("#ddNhaCungCap option:selected").val(), $("#ddlKho option:selected").val(),
            $("#ddlVatTu option:selected").val(), $("#ddlLoSanXuat option:selected").val(),
            $("#ddlPhieuNhap option:selected").val());
        if (data.length > 0) {
            $("#txtDonGia").val(data[0].DonGia);
            $("#txtHanSuDung").data("DateTimePicker").setDate(moment(data[0].HanSuDung).format("DD-MM-YYYY"));
        } else {
            $("#txtDonGia").val("");
            $("#txtHanSuDung").val("");
        }
        $("#txtSoLuongTon").val(CheckTon_Ma_Lo_PhieuNhap($("#ddlVatTu option:selected").val(),
            $("#ddlLoSanXuat option:selected").val(), $("#ddlPhieuNhap option:selected").val(),
            $("#ddlKho option:selected").val()));
        $("#txtSoLuong").focus();
    });
    // Change tab
    $("#ul_tab_phieutranhacungcap a[href=\"#tab_tranhacungcap\"]").on("shown.bs.tab", function (e) {
        $("#txtNguoiTra").focus();
    });
    $("#ul_tab_phieutranhacungcap a[href=\"#tab_tranhacungcap_chitiet\"]").on("shown.bs.tab", function (e) {
        $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
    });
});
function loadcombobox() {
    filldropdown("#ddThue", SelectAllThue());
    filldropdown("#ddlKho", superadmin === true ? SelectAllKho() : SelectKhoByKhoa(makhoa));
    filldropdown("#ddNhaCungCap", SelectAllNhaCungCap());
    filldropdown("#ddDonVi", SelectAllDonVi());
    $("#ddDonVi").find("[value=\"\"]").text("");
    $("#ddDonVi").selectpicker("refresh");
}
function validationform_Timkiem() {
    $("#txtTuNgay").rules("add", {
        required: true,
        messages: {
            required: MsErrorEmpty
        }
    });
    $("#txtDenNgay").rules("add", {
        required: true,
        messages: {
            required: MsErrorEmpty
        }
    });
}
function themphieutranhacungcap() {
    if (CheckSession() === "") { window.location = "/"; return; }
    loadcombobox();
    $("#ul_tab_phieutranhacungcap a[href=\"#tab_tranhacungcap\"]").tab("show");
    loaddanhsachvattu_phieutranhacungcap(matranhacungcap);
    $("#div_phieutranhacungcap").modal("show");
}
function suaphieutranhacungcap(tagButton) {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#btnNhapLaiPhieu").hide();
    loadcombobox();
    matranhacungcap = $(tagButton).attr("maphieu");
    if (matranhacungcap === "") return;
    var dataPhieuTraNhaCungCap = PhieuTraNhaCungCap_SelectById(matranhacungcap);
    $("#ddlKho").selectpicker("val", dataPhieuTraNhaCungCap[0].MaKho);
    $("#ddNhaCungCap").selectpicker("val", dataPhieuTraNhaCungCap[0].MaNhaCungCap);
    (dataPhieuTraNhaCungCap[0].NgayTra == null || dataPhieuTraNhaCungCap[0].NgayTra === "")
        ? $("#txtNgayTra").val("")
        : $("#txtNgayTra").data("DateTimePicker").setDate(moment(dataPhieuTraNhaCungCap[0].NgayTra).format("DD-MM-YYYY"));
    $("#txtNguoiTra").val(dataPhieuTraNhaCungCap[0].NguoiTra);
    $("#txtNguoiNhan").val(dataPhieuTraNhaCungCap[0].NguoiNhan);
    $("#ddThue").selectpicker("val", dataPhieuTraNhaCungCap[0].Thue);
    $("#txtDienGiai").val(dataPhieuTraNhaCungCap[0].DienGiai);
    $("#ddlKho").prop("disabled", true);
    $("#ddlKho").selectpicker("refresh");
    $("#ddNhaCungCap").prop("disabled", true);
    $("#ddNhaCungCap").selectpicker("refresh");
    filldropdown("#ddlVatTu", PhieuTraNhaCungCap_VatTuByNCC($("#ddNhaCungCap option:selected").val(), $("#ddlKho option:selected").val()));
    loaddanhsachvattu_phieutranhacungcap(matranhacungcap);
    $("#ul_tab_phieutranhacungcap a[href=\"#tab_tranhacungcap\"]").tab("show");
    $("#div_phieutranhacungcap").modal("show");
}
function luuphieutranhacungcap() {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#txtNgayTra, #txtNguoiTra, #txtNguoiNhan, #ddThue, #ddKho, #ddNhaCungCap").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    if (!$("#form_phieutranhacungcap_thongtinphieu").valid()) return;
    if (matranhacungcap === "") {
        matranhacungcap = PhieuTraNhaCungCap_Insert(
            $("#ddlKho option:selected").val(), $("#ddNhaCungCap option:selected").val(),
            $("#txtNgayTra").val(), $("#txtNguoiTra").val(), $("#txtNguoiNhan").val(),
            $("#ddThue option:selected").val(), $("#txtDienGiai").val(), username);
        if (matranhacungcap === "") {
            $("#txtNguoiTra").focus();
            return;
        }
    } else {
        PhieuTraNhaCungCap_Update(
            $("#ddlKho option:selected").val(), matranhacungcap, $("#ddNhaCungCap option:selected").val(),
            $("#txtNgayTra").val(), $("#txtNguoiTra").val(), $("#txtNguoiNhan").val(),
            $("#ddThue option:selected").val(), $("#txtDienGiai").val(), username);
    }
    $("#ddlKho").prop("disabled", true);
    $("#ddlKho").selectpicker("refresh");
    $("#ddNhaCungCap").prop("disabled", true);
    $("#ddNhaCungCap").selectpicker("refresh");
    $("#btnNhapLaiPhieu").hide();
    filldropdown("#ddlVatTu", PhieuTraNhaCungCap_VatTuByNCC($("#ddNhaCungCap option:selected").val(), $("#ddlKho option:selected").val()));
    $("#ul_tab_phieutranhacungcap a[href=\"#tab_tranhacungcap_chitiet\"]").tab("show");
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
}
function resetphieutranhacungcap() {
    ResetControlForm("#form_phieutranhacungcap_thongtinphieu");
    $("#txtNguoiTra").focus();
}
function suaphieutranhacungcap_chitiet(tagInput) {
    if (CheckSession() === "") { window.location = "/"; return; }
    var data = PhieuTraNhaCungCap_ChiTiet_SelectById($(tagInput).attr("matranhacungcap_chitiet"));
    if (data.length <= 0) {
        bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        return;
    }
    $("#ul_tab_phieutranhacungcap a[href=\"#tab_tranhacungcap_chitiet\"]").tab("show");
    matranhacungcap_chitiet = data[0].ID;
    $("#ddlVatTu").selectpicker("val", data[0].MaVatTu).change();
    $("#ddlLoSanXuat").selectpicker("val", data[0].LoSanXuat).change();
    $("#ddlPhieuNhap").selectpicker("val", data[0].MaPhieuNhap).change();
    $("#txtSoLuong").val(data[0].SoLuong);
}
function luuphieutranhacungcap_chitiet() {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#ddlVatTu, #ddlLoSanXuat, #ddlPhieuNhap, #txtSoLuong").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    $("#txtSoLuong").rules("add", {
        larger_ton: "#txtSoLuongTon"
    });
    if (!$("#form_phieutranhacungcap_chitietphieu").valid()) return;
    if (matranhacungcap !== "") {
        if (matranhacungcap_chitiet === "") {
            PhieuTraNhaCungCap_ChiTiet_Insert(
                matranhacungcap, $("#ddlVatTu option:selected").val(), $("#ddDonVi option:selected").val(),
                $("#txtSoLuong").val(), $("#txtDonGia").val(), $("#ddlLoSanXuat option:selected").val(),
                $("#txtHanSuDung").val(), $("#ddlPhieuNhap option:selected").val(), username
            );
        } else {
            PhieuTraNhaCungCap_ChiTiet_Update(
                matranhacungcap_chitiet, matranhacungcap, $("#ddlVatTu option:selected").val(),
                $("#ddDonVi option:selected").val(), $("#txtSoLuong").val(), $("#txtDonGia").val(),
                $("#ddlLoSanXuat option:selected").val(), $("#txtHanSuDung").val(),
                $("#ddlPhieuNhap option:selected").val(), username
            );
        }
        loaddanhsachvattu_phieutranhacungcap(matranhacungcap);
        resetphieutranhacungcapchitiet();
    } else {
        bootbox.alert("Bạn chưa tạo phiếu trả nhà cung cấp, không thể thêm vật tư mới!");
        $("#txtNguoiGiao").focus();
    }
}
function xoaphieutranhacungcap_chitiet(tagInput, multiDelete) {
    if (CheckSession() === "") { window.location = "/"; return; }
    if (multiDelete) {
        var arrId = $("#tb_phieutranhacungcap_chitiet").bootstrapTable("getSelections");
        if (arrId.length > 0) {
            bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
                if (result) {
                    $.each(arrId, function (key, value) {
                        PhieuTraNhaCungCap_ChiTiet_Delete(value[1]);
                    });
                    loaddanhsachvattu_phieutranhacungcap(matranhacungcap);
                }
            });
        } else {
            bootbox.alert("Bạn cần chọn bản ghi trước khi xóa!");
        }
    } else {
        bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
            if (result) {
                PhieuTraNhaCungCap_ChiTiet_Delete($(tagInput).attr("matranhacungcap_chitiet"));
                loaddanhsachvattu_phieutranhacungcap(matranhacungcap);
            }
        });
    }
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
}
function loaddanhsachvattu_phieutranhacungcap(matranhacungcap) {
    var data = PhieuTraNhaCungCap_ChiTiet_SelectByMaPhieu(matranhacungcap);
    $("#tb_phieutranhacungcap_chitiet").bootstrapTable("destroy");
    $("#tbody_phieutranhacungcap_chitiet").empty();
    var str = "";
    $.each(data, function (key, value) {
        str += "<tr>";
        str += "<td></td>";
        str += "<td>" + value.ID + "</td>";
        str += "<td>" + value.TenVatTu + "</td>";
        str += "<td>" + value.TenDonVi + "</td>";
        str += "<td>" + $.number(value.DonGia, 0, ".", ",") + "</td>";
        str += "<td>" + $.number(value.SoLuong, 0, ".", ",") + "</td>";
        str += "<td>" + value.LoSanXuat + "</td>";
        str += "<td>" + value.MaPhieuNhap + "</td>";
        str += "<td>" +
                    "<button type=\"button\" id=\"btnSuaPhieuTraNhaCungCap_ChiTiet\" title=\"Sửa\" matranhacungcap_chitiet=\"" + value.ID + "\" onclick=\"suaphieutranhacungcap_chitiet(this)\" class=\"btn btn-info\">" +
                        "<span class=\"glyphicon glyphicon-edit\"></span>" +
                    "</button>" +
                    "&nbsp;" +
                    "<button type=\"button\" id=\"btnXoaPhieuTraNhaCungCap_ChiTiet\" title=\"Xóa\" matranhacungcap_chitiet=\"" + value.ID + "\" onclick=\"xoaphieutranhacungcap_chitiet(this, false)\" class=\"btn btn-danger\">" +
                        "<span class=\"glyphicon glyphicon-trash\"></span>" +
                    "</button>" +
               "</td>";
        str += "</tr>";
    });
    $("#tbody_phieutranhacungcap_chitiet").append(str);
    $("#tb_phieutranhacungcap_chitiet").bootstrapTable({
        classes: "table table-condensed",
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        }
    });
}
function resetphieutranhacungcapchitiet() {
    ResetControlForm("#form_phieutranhacungcap_chitietphieu");
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
    filldropdown("#ddlLoSanXuat", []);
    filldropdown("#ddlPhieuNhap", []);
    matranhacungcap_chitiet = "";
}
function viewphieutranhacungcap(tagButton) {
    matranhacungcap = $(tagButton).attr("maphieu");
    if (matranhacungcap === "") return;
    var dataPhieuTraNhaCungCap = PhieuTraNhaCungCap_SelectById(matranhacungcap);
    (dataPhieuTraNhaCungCap[0].NgayTra == null || dataPhieuTraNhaCungCap[0].NgayTra === "")
        ? $("#txtNgayTra_ViewPhieuTraNhaCungCap").val("")
        : $("#txtNgayTra_ViewPhieuTraNhaCungCap").val(moment(dataPhieuTraNhaCungCap[0].NgayTra).format("DD-MM-YYYY"));
    $("#txtNguoiTra_ViewPhieuTraNhaCungCap").val(dataPhieuTraNhaCungCap[0].NguoiTra);
    $("#txtNguoiNhan_ViewPhieuTraNhaCungCap").val(dataPhieuTraNhaCungCap[0].NguoiNhan);
    $("#txtThue_ViewPhieuTraNhaCungCap").val(dataPhieuTraNhaCungCap[0].GT_Thue);
    $("#txtKho_ViewPhieuTraNhaCungCap").val(dataPhieuTraNhaCungCap[0].TenKho);
    $("#txtNhaCungCap_ViewPhieuTraNhaCungCap").val(dataPhieuTraNhaCungCap[0].TenNhaCungCap);
    $("#txtDienGiai_ViewPhieuTraNhaCungCap").val(dataPhieuTraNhaCungCap[0].DienGiai);
    // Thông tin thuốc-vật tư
    var data = PhieuTraNhaCungCap_ChiTiet_SelectByMaPhieu(matranhacungcap);
    $("#tb_phieutranhacungcap_chitiet_view").bootstrapTable("destroy");
    $("#tbody_phieutranhacungcap_chitiet_view").empty();
    var str = "";
    $.each(data, function (key, value) {
        str += "<tr>";
        str += "<td>" + value.TenVatTu + "</td>";
        str += "<td>" + value.TenDonVi + "</td>";
        str += "<td>" + $.number(value.DonGia, 0, ".", ",") + "</td>";
        str += "<td>" + $.number(value.SoLuong, 0, ".", ",") + "</td>";
        str += "<td>" + value.LoSanXuat + "</td>";
        str += "<td>" + value.MaPhieuNhap + "</td>";
        str += "</tr>";
    });
    $("#tbody_phieutranhacungcap_chitiet_view").append(str);
    $("#tb_phieutranhacungcap_chitiet_view").bootstrapTable({
        classes: "table table-condensed",
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        }
    });
    $("#div_phieutranhacungcap_view").modal("show");
}
function PhieuTraNhaCungCap_Insert(makho, manhacungcap, ngaytra, nguoitra, nguoinhan, thue, diengiai, nguoitao) {
    var maphieu = "";
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_Insert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makho': '" + makho + "', 'manhacungcap': " + (manhacungcap === "" ? 0 : parseInt(manhacungcap)) + "" +
            ", 'ngaytra': '" + ngaytra + "', 'nguoitra': '" + nguoitra + "', 'nguoinhan': '" + nguoinhan + "'" +
            ", 'thue': " + (thue === "" ? 0 : parseInt(thue)) + "" +
            ", 'diengiai': '" + diengiai + "', 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function (n) {
            maphieu = n.d;
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return maphieu;
}
function PhieuTraNhaCungCap_Update(makho, maphieu, manhacungcap, ngaytra, nguoitra, nguoinhan, thue, diengiai, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_Update",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makho': '" + makho + "', 'maphieu': '" + maphieu + "'" +
            ", 'manhacungcap': '" + (manhacungcap === "" ? 0 : parseInt(manhacungcap)) + "', 'ngaytra': '" + ngaytra + "'" +
            ", 'nguoitra': '" + nguoitra + "', 'nguoinhan': '" + nguoinhan + "', 'thue': " + (thue === "" ? 0 : parseInt(thue)) + "" +
            ", 'diengiai': '" + diengiai + "', 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuTraNhaCungCap_SelectById(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_SelectById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_VatTuByNCC(manhacungcap, makho) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_VatTuByNCC",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'manhacungcap': " + manhacungcap + ", 'makho': '" + makho + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_LoSanXuat_ByMaVatTu(manhacungcap, makho, mavattu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_LoSanXuat_ByMaVatTu",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'manhacungcap': " + manhacungcap + ", 'makho': '" + makho + "', 'mavattu': '" + mavattu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_MaPhieuNhap_By_LoSanXuat(manhacungcap, makho, mavattu, losanxuat) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_MaPhieuNhap_By_LoSanXuat",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'manhacungcap': " + manhacungcap + ", 'makho': '" + makho + "'" +
            ", 'mavattu': '" + mavattu + "', 'losanxuat': '" + losanxuat + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_DonGia_ByMaPhieuNhap(manhacungcap, makho, mavattu, losanxuat, maphieunhap) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_DonGia_ByMaPhieuNhap",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'manhacungcap': " + manhacungcap + ", 'makho': '" + makho + "', 'mavattu': '" + mavattu + "', " +
              "'losanxuat': '" + losanxuat + "', 'maphieunhap': '" + maphieunhap + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_ChiTiet_Insert(maphieu, mavattu, madonvi, soluong, dongia, losanxuat, hansudung, maphieunhap, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_ChiTiet_Insert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "', 'madonvi': '" + madonvi + "'" +
            ", 'soluong': " + parseInt(soluong) + ", 'dongia': " + parseFloat(dongia) + "" +
            ", 'losanxuat': '" + losanxuat + "', 'hansudung': '" + hansudung + "', 'maphieunhap': '" + maphieunhap + "'" +
            ", 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuTraNhaCungCap_ChiTiet_Update(id, maphieu, mavattu, madonvi, soluong, dongia, losanxuat, hansudung, maphieunhap, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_ChiTiet_Update",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id': '" + id + "', 'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "'" +
            ", 'madonvi': '" + madonvi + "', 'soluong': " + parseInt(soluong) + ", 'dongia': " + parseFloat(dongia) +
            ", 'losanxuat': '" + losanxuat + "', 'hansudung': '" + hansudung + "', 'maphieunhap': '" + maphieunhap + "'" +
            ", 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuTraNhaCungCap_ChiTiet_SelectById(id) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_ChiTiet_SelectById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id': '" + id + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_ChiTiet_SelectByMaPhieu(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_ChiTiet_SelectByMaPhieu",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuTraNhaCungCap_ChiTiet_Delete(id) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuTraNhaCungCap.asmx/PhieuTraNhaCungCap_ChiTiet_Delete",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id': '" + id + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}